﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.IO;
using Google.GData.Client;
using Google.GData.Documents;
using Google.GData.Extensions;
using Google.Documents;
using System.Windows.Forms;
using Google.GData.Apps;
using System.ComponentModel;
using System.Threading;
using System.Diagnostics;

namespace GDocUploader.Communication
{
    class FileS
    {
        private Feed<Document> Fed = null;

        public FileS(String chemin)
        {
            if ((Operations.isFile(chemin)) && (new FileInfo(chemin).Extension.Length == 0)) CreateFile(new FileInfo(chemin).Name, File.Open(chemin, FileMode.Open));
            else
            Upload_File(chemin, new FileInfo(chemin).Name);
        }

        public FileS(String Chemin, String PathTo)
        {
            try
            {
                Stream s = File.Open(Chemin, FileMode.Open);
                s.Close();
                s.Dispose();
            }
            catch
            {
             
            }
            finally
            {
                CreateFile(new FileInfo(Chemin).Name, null, PathTo);
               
            }
           
        }

        public FileS(String chemin, Boolean withoutExtention)
        {
            try
            {
                Stream s = File.Open(chemin, FileMode.Open);
                s.Close();
                s.Dispose();
            }
            catch
            {
             
            }
            finally
            {
                CreateFile(new FileInfo(chemin).Name, null);
              
            }
        }

        public FileS(String chemin, String PathTo, Boolean withoutExtention)
        {
            try
            {
                Stream s = File.Open(chemin, FileMode.Open);
                if (!EventLog.SourceExists("GDOCUploader"))
                    EventLog.CreateEventSource("GDOCUploader", "Application");
                EventLog.WriteEntry("GDOCUploader", "Lecture du stream de " + chemin);

             
                 s.Close();
                 s.Dispose();
            }
            catch
            {
                if (!EventLog.SourceExists("GDOCUploader"))
                    EventLog.CreateEventSource("GDOCUploader", "Application");
                EventLog.WriteEntry("GDOCUploader", "Erreur de lecture de stream de " + chemin);

           
            }
            finally
            {
                CreateFile(new FileInfo(chemin).Name, null, PathTo, new bool());
               
            }
        }

        private void CreateFile(String Nom_File, Stream Stre, String path_File, bool withoutextention)
        {
            Authentification.login();
                if (path_File.EndsWith("/")) path_File = path_File.Remove(path_File.Length - 1);
                String[] Lst_Folder = path_File.Split('/');
                String id = "";
                string parent = Lst_Folder[Lst_Folder.Length - 1];
              
                        DocumentsRequest Rs = new DocumentsRequest(new RequestSettings("GDocUploader", frmUploader.ServGen.Credentials));
                        Fed = Rs.GetEverything();
                    foreach (Document d in Fed.Entries)
                    {
                        Application.DoEvents();
                        if (d.Title == parent)
                        {
                            id = d.DocumentId;
                            break;
                        }
                    }

                    //
                //envoyer le fichier
                try
                {
                    AtomCategory category = new AtomCategory("http://schemas.google.com/docs/2007#document", new AtomUri("http://schemas.google.com/g/2005#kind"));
                    category.Label = "document";
                    AtomEntry folder = new AtomEntry();
                    folder.Categories.Add(category);
                    folder.Title = new AtomTextConstruct(AtomTextConstructElementType.Title, Nom_File);
                    Uri feedUri = new Uri("http://docs.google.com/feeds/folders/private/full/folder%3A"+ id);
                    Stream str = frmUploader.ServGen.EntrySend(feedUri, folder, GDataRequestType.Insert);
                    str.Close();
                    str.Dispose();
                }
                catch
                {
                }
                try
                {
                    
                        Fed = Rs.GetEverything();
                   
                    foreach (Document d in Fed.Entries)
                    {
                        if (!d.DocumentEntry.IsFolder)
                        {
                            Application.DoEvents();
                            if (d.Title.Equals(Nom_File))
                            {
                                ((GDataRequestFactory)frmUploader.ServGen.RequestFactory).CustomHeaders.Add(GDataRequestFactory.IfMatch + ":*");
                                frmUploader.ServGen.Update(new Uri("http://docs.google.com/feeds/media/private/full/document%3A" + d.DocumentId), Stre, "Plain text/.txt", Nom_File);
                                ((GDataRequestFactory)frmUploader.ServGen.RequestFactory).CustomHeaders.Remove(GDataRequestFactory.IfMatch + ":*");
                            }

                        }
                    }
                }
                catch
                {
                }

                try
                {
                    Stre.Close();
                    Stre.Dispose();

                }
                catch
                {

                }
                clear();
            }

        private void CreateFile(String Nom_File, Stream stre)
        {
            Authentification.login();
            try
            {
                try
                {

                    AtomCategory category = new AtomCategory("http://schemas.google.com/docs/2007#document", new AtomUri("http://schemas.google.com/g/2005#kind"));
                    category.Label = "document";
                    AtomEntry folder = new AtomEntry();
                    folder.Categories.Add(category);
                    folder.Title = new AtomTextConstruct(AtomTextConstructElementType.Title, Nom_File);
                    Uri feedUri = new Uri("http://docs.google.com/feeds/documents/private/full");
                    Stream str = frmUploader.ServGen.EntrySend(feedUri, folder, GDataRequestType.Insert);
                    str.Close();
                    str.Dispose();
                }
                catch
                {
                   
                }
                try
                {
                   
                        DocumentsRequest Rs = new DocumentsRequest(new RequestSettings("GDocUploader", frmUploader.ServGen.Credentials));
                        Fed = Rs.GetEverything();
                  
                    foreach (Document d in Fed.Entries)
                    {
                        if (!d.DocumentEntry.IsFolder)
                        {

                            Application.DoEvents();
                            if (d.Title.Equals(Nom_File))
                            {
                                ((GDataRequestFactory)frmUploader.ServGen.RequestFactory).CustomHeaders.Add(GDataRequestFactory.IfMatch + ":*");
                                frmUploader.ServGen.Update(new Uri("http://docs.google.com/feeds/media/private/full/document%3A" + d.DocumentId), stre, "Plain text/.txt", Nom_File);
                                ((GDataRequestFactory)frmUploader.ServGen.RequestFactory).CustomHeaders.Remove(GDataRequestFactory.IfMatch + ":*");
                            }
                        }


                    }
                }
                catch
                {
                }

                try
                {
                    stre.Close();
                    stre.Dispose();

                }
                catch
                {

                }

            }
            catch
            {
            }
            clear();
        }

        private void CreateFile(String Nom_File, Stream Stre,  String path_File)
        {
            Authentification.login();
            try
            {
                if (path_File.EndsWith("/")) path_File = path_File.Remove(path_File.Length - 1);
                String[] Lst_Folder = path_File.Split('/');
                String id = "";
                string parent = Lst_Folder[Lst_Folder.Length - 1];
               
                    System.Windows.Forms.Application.DoEvents();
                    DocumentsRequest Rs = new DocumentsRequest(new RequestSettings("GDocUploader", frmUploader.ServGen.Credentials));
                    Fed = Rs.GetEverything();
                 
                    foreach (Document d in Fed.Entries)
                    {
                        Application.DoEvents();
                        if (d.Title == parent)
                        {
                            id = d.DocumentId;
                            break;
                        }
                    }

              
                try
                {

                    AtomCategory category = new AtomCategory("http://schemas.google.com/docs/2007#document", new AtomUri("http://schemas.google.com/g/2005#kind"));
                    category.Label = "document";
                    AtomEntry folder = new AtomEntry();
                    folder.Categories.Add(category);
                    folder.Title = new AtomTextConstruct(AtomTextConstructElementType.Title, Nom_File);
                    Uri feedUri = new Uri("http://docs.google.com/feeds/folders/private/full/folder%3A" + id);
                    Stream str = frmUploader.ServGen.EntrySend(feedUri, folder, GDataRequestType.Insert);
                    str.Close();
                    str.Dispose();
                }
                catch
                {
                   
                }
                try
                {
                   
                    foreach (Document d in Fed.Entries)
                    {
                        if (d.DocumentEntry.IsFolder)
                        {
                            Application.DoEvents();
                            if (d.Title.Equals(Nom_File))
                            {
                                ((GDataRequestFactory)frmUploader.ServGen.RequestFactory).CustomHeaders.Add(GDataRequestFactory.IfMatch + ":*");
                                frmUploader.ServGen.Update(new Uri("http://docs.google.com/feeds/media/private/full/document%3A" + d.DocumentId), Stre, "Plain text/.txt", Nom_File);
                                ((GDataRequestFactory)frmUploader.ServGen.RequestFactory).CustomHeaders.Remove(GDataRequestFactory.IfMatch + ":*");
                            }
                        }

                    }
                }
                catch
                {
                }

                try
                {
                    Stre.Close();
                    Stre.Dispose();

                }
                catch
                {

                }
            }
            catch
            {
            }
            clear();
            
        }

        private void Upload_File(String chemin, String nom)
        {
            Authentification.login();
            try
            {
                DocumentEntry newEntry = frmUploader.ServGen.UploadDocument(chemin, nom);
            }
            catch
            {
             
            }
            clear();

        }

        internal void clear()
        {
            finnnn:
            try
            {
                Authentification.login();
                DocumentsListQuery query = new DocumentsListQuery();
                query.ShowFolders = false;
                DocumentsFeed feed = frmUploader.ServGen.Query(query);
                foreach (DocumentEntry D in feed.Entries)
                {
                    if (D.Title.Text == "blank")
                    {
                        D.Delete();
                    }
                }
            }
            catch
            {
                goto finnnn;
            }
            
        }
    }
}
